<?php

defined('SYSPATH') or die('Access error!');

class Model_User_Message extends Model
{

    public $rules = array(
        'to_user' => array
            (
            'not_empty' => null,
        ),
        'content' => array
            (
            'not_empty' => null,
        )
    );

    public function get_message_list($where_sql = '', $user_type = 'from_user', $nums = 10)
    {
        if (Arr::validate_arr($where_sql))
        {
            $where_sql = $this->get_table_where('um', $where_sql);
        }
        $sql = 'SELECT um.*,u.username,u.avatar,u.id as user_id ' .
                'FROM ' . $this->table . ' AS um ' .
                'LEFT JOIN ' . $this->db->get_table('user') . ' AS u ON um.' . $user_type . ' = u.id ' . $where_sql;
        $data = $this->get_page_list($sql, $nums);
        $temp = $data['list'];
        $data_list = array();
        if (Arr::validate_arr($temp))
        {
            foreach ($temp as $value)
            {
                $value['avatar'] = Model::factory('user')->create_avatar_path($value['user_id'], $value['avatar']);
                $data_list[] = $value;
            }
        }
        $data['list'] = $data_list;
        return $data;
    }

    public function get_message_detail($message_id)
    {
        $sql = 'SELECT um.*,u.username,u.id as user_id,u.avatar ' .
                'FROM ' . $this->table . ' AS um ' .
                'LEFT JOIN ' . $this->db->get_table('user') . ' AS u ON um.from_user = u.id ' .
                ' WHERE um.id=' . $message_id;
        $message_detail = $this->db->get_row($sql, ARRAY_A);

        $message_detail['avatar_path'] = Model::factory('user')->create_avatar_path($message_detail['user_id'], $message_detail['avatar']);
        if(!empty($message_detail['url'])){
            $message_detail['url'] = URL::site($message_detail['url']);
        }
        return $message_detail;
    }

    public function get_message_all_children($message_id)
    {
        $all_children_id = $this->get_all_children($message_id);

        $in_sql = $this->get_in($all_children_id);

        $sql = "SELECT um.id,um.parent_id,um.from_user,um.content,um.add_time,u.username,u.avatar,u.id as user_id " .
                " FROM " . $this->table . ' AS um ' .
                'LEFT JOIN ' . $this->db->get_table('user') . ' AS u ON um.from_user = u.id WHERE um.id ' . $in_sql;
        $message_list_temp = $this->db->get_results($sql, ARRAY_A);
        $message_list = array();
        if (Arr::validate_arr($message_list_temp))
        {
            foreach ($message_list_temp as $value)
            {
                $value['avatar_path'] = Model::factory('user')->create_avatar_path($value['user_id'], $value['avatar']);
                $message_list[] = $value;
            }
        }
        return $message_list;
    }

    public function send_message($message_arr = array())
    {
        $data = array();
        $data['title'] = HTML::entities(Arr::get($message_arr, 'title', ''));
        $data['content'] = HTML::entities(Arr::get($message_arr, 'content', ''));
        $data['to_user'] = intval(Arr::get($message_arr, 'to_user', ''));
        $data['from_user'] =intval(Arr::get($message_arr, 'from_user', ''));
        $data['url'] = HTML::entities(Arr::get($message_arr, 'url', ''));
        $data['parent_id'] = intval(Arr::get($message_arr, 'parent_id', ''));
        $data['add_time'] = Date::get_time();
        $data['update_time'] = Date::get_time();
        return $this->insert($data);
    }

    /**
     * 取得某分类的所有子孙分类id
     */
    public function get_all_children($id)
    {
        $this->all_children[] = $id;
        $this->_get_all_children($id);
        return $this->all_children;
    }

    /**
     *  递归调用所有子分类（用于小数据）
     * @param type $id
     */
    private function _get_all_children($id)
    {
        $childs = $this->get_results(' AND parent_id=' . $id, 'id');
        foreach ($childs as $child)
        {
            $this->all_children[] = intval($child['id']);
            $this->_get_all_children($child['id']);
        }
    }

}